package io.adrainty.bolee.insurance.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.adrainty.bolee.framework.commons.basic.ResultDTO;
import io.adrainty.bolee.framework.commons.utils.ResultDTOUtil;
import io.adrainty.bolee.insurance.models.entity.SafeguardEntity;
import io.adrainty.bolee.insurance.service.ISafeguardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author AdRainty
 * @version V1.0.0
 * @since 2025/8/10 下午9:36
 */

@Tag(name = "保障项管理")
@RestController
@RequestMapping("/safeguard")
public class SafeguardController {

    @Resource
    private ISafeguardService safeguardService;

    @PostMapping("/page/{pageNum}/{pageSize}")
    @Operation(summary = "分页获取保障项")
    public ResultDTO<IPage<SafeguardEntity>> findSafeguardPage(@PathVariable("pageNum") Integer pageNum,
                                                               @PathVariable("pageSize") Integer pageSize,
                                                               @RequestBody SafeguardEntity searchDTO) {
        IPage<SafeguardEntity> pageResult = safeguardService.findSafeguardPage(pageNum, pageSize, searchDTO);
        return ResultDTOUtil.success(pageResult);
    }

    @PostMapping("/list")
    @Operation(summary = "获取保障项列表")
    public ResultDTO<List<SafeguardEntity>> findSafeguardList(@RequestBody SafeguardEntity entity) {
        List<SafeguardEntity> list = safeguardService.findSafeguardList(entity);
        return ResultDTOUtil.success(list);
    }

    @PutMapping
    @Operation(summary = "创建保障项")
    public ResultDTO<SafeguardEntity> createSafeguard(@RequestBody SafeguardEntity entity) {
        SafeguardEntity result = safeguardService.createSafeguard(entity);
        return ResultDTOUtil.success(result);
    }

    @PatchMapping
    @Operation(summary = "修改保障项")
    public ResultDTO<Boolean> updateSafeguard(@RequestBody SafeguardEntity entity) {
        Boolean result = safeguardService.updateSafeguard(entity);
        return ResultDTOUtil.success(result);
    }

    @DeleteMapping
    @Operation(summary = "删除保障项")
    public ResultDTO<Boolean> deleteSafeguard(@RequestBody SafeguardEntity entity) {
        Boolean result = safeguardService.deleteSafeguard(Arrays.stream(entity.getCheckedIds()).toList());
        return ResultDTOUtil.success(result);
    }

}
